<?php
require_once('../config.inc');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Could not connect: " . mysql_error());
mysql_select_db(DB_NAME, $link) or die ('Can\'t use foo : ' . mysql_error());


$r = mysql_query('SELECT * FROM meccano_gallery');
for($i=0, $t=mysql_num_rows($r); $i<$t; $i++)
{
   $row = mysql_fetch_assoc($r);

   $image = array();
   if(!empty($row['gls_src']) && file_exists(ROOT . $row['gls_src']))
   {
      $image[0] = getimagesize(ROOT . $row['gls_src']);
      $image[0]['src'] = $row['gls_src'];
   }
   if(!empty($row['glm_src']) && file_exists(ROOT . $row['glm_src']))
   {
      $image[1] = getimagesize(ROOT . $row['glm_src']);
      $image[1]['src'] = $row['glm_src'];
   }
   if(!empty($row['gll_src']) && file_exists(ROOT . $row['gll_src']))
   {
      $image[2] = getimagesize(ROOT . $row['gll_src']);
      $image[2]['src'] = $row['gll_src'];
   }

   mysql_query('UPDATE meccano_gallery SET image = "'. mysql_real_escape_string(serialize($image)) .'" WHERE id ='. $row['id'] .' AND lang ='.$row['lang']);
}


$r = mysql_query('SELECT DISTINCT id, image1, image2, image3 FROM meccano_articles');

for($i=0, $t=mysql_num_rows($r); $i<$t; $i++)
{
   $row = mysql_fetch_assoc($r);
   $images = array();

   if(!empty($row['image1']) && ($tmp = unserialize($row['image1'])))
   {
      if(file_exists(ROOT . $tmp['src']))
      {
         $images[0] = getimagesize(ROOT . $tmp['src']);
         $images[0]['src'] = $tmp['src'];
      }
   }
   if(!empty($row['image2']) && ($tmp = unserialize($row['image2'])))
   {
      if(file_exists(ROOT . $tmp['src']))
      {
         $images[1] = getimagesize(ROOT . $tmp['src']);
         $images[1]['src'] = $tmp['src'];
      }
   }
   if(!empty($row['image3']) && ($tmp = unserialize($row['image3'])))
   {
      if(file_exists(ROOT . $tmp['src']))
      {
         $images[2] = getimagesize(ROOT . $tmp['src']);
         $images[2]['src'] = $tmp['src'];
      }
   }

   mysql_query('UPDATE meccano_articles SET images = "'. mysql_real_escape_string(serialize($images)) .'" WHERE id = '. $row['id']);
}

$r = mysql_query('SELECT id, image1, image2, image3 FROM meccano_tree');

for($i=0, $t=mysql_num_rows($r); $i<$t; $i++)
{
   $row = mysql_fetch_assoc($r);
   $images = array();

   if(!empty($row['image1']) && ($tmp = unserialize($row['image1'])))
   {
      if(file_exists(ROOT . $tmp['src']))
      {
         $images[0] = getimagesize(ROOT . $tmp['src']);
         $images[0]['src'] = $tmp['src'];
      }
   }
   if(!empty($row['image2']) && ($tmp = unserialize($row['image2'])))
   {
      if(file_exists(ROOT . $tmp['src']))
      {
         $images[1] = getimagesize(ROOT . $tmp['src']);
         $images[1]['src'] = $tmp['src'];
      }
   }
   if(!empty($row['image3']) && ($tmp = unserialize($row['image3'])))
   {
      if(file_exists(ROOT . $tmp['src']))
      {
         $images[2] = getimagesize(ROOT . $tmp['src']);
         $images[2]['src'] = $tmp['src'];
      }
   }

   mysql_query('UPDATE meccano_tree SET images = "'. mysql_real_escape_string(serialize($images)) .'" WHERE id = '. $row['id']);
}